<?php
/**
 * 文件名brandslide.mod.php
 * 版本号：1.0
 * 作者：zhangbaofeng  2012-03-29
 * 功能描述: 品牌分类
 */

if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject
{
	var $TopicLogic;	

	function ModuleObject($config)
	{
		$this->MasterObject($config);
		
		Load::logic('topic');
		$this->TopicLogic = new TopicLogic($this);
		
		Load::lib('form');
		$this->FormHandler = new FormHandler;
		
		$this->CacheConfig = ConfigHandler::get('cache');
		
		
		$this->Execute();
	}

	
	function Execute()
	{
		ob_start();
		switch($this->Code)
		{	
			case 'delete':
				$this->Delete();
				break;			
			case 'add':
				$this->Add();
				break;
			case 'modify':
				$this->Modify();
				break;
		   case 'domodify':
				$this->DoModify();
				break;
		  case 'addcom':
		  		$this->addcom();
				break;
		  case 'addtob':
		       $this->addtob();
				break;
		 case 'tobaccoindex':
		       $this->tobaccoindex();
				break;
		 case 'deletetob':
		       $this->deletetob();
				break;
		 case 'edittob':
		       $this->edittob();
				break;
		 case 'liuyan':
		       $this->liuyan();
				break;
			default:
				$this->Main();
				break;
		}
		$body = ob_get_clean();
		
		$this->ShowBody($body);
		
	}

	
	function Main()
	{ 
		
		$per_page_num = min(500,max((int) $_GET['per_page_num'],(int) $_GET['pn'],10));
		$query_link = 'admin.php?mod=brandtype';
		$sql = " select count(*) as `total_record` from `".TABLE_PREFIX."brandclass` ";	
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$page_arr = page($total_record,$per_page_num,$query_link,array('return'=>'array',),'20 50 100 200,500');
		$sql = "select * from `".TABLE_PREFIX."brandclass` ";
	 	$query = $this->DatabaseHandler->Query($sql);
	    $brandtype_list=array();
		while($row = $query->GetRow())
		{	
			$brandtype_list[]=$row;
		}
		//echo dirname(dirname(dirname(__FILE__)));die();
		include($this->TemplateHandler->Template('admin/brandtype'));
	}

	function Add()
	{		
		$picurl1 = $this->Post['brandpic1'];
		$picurl2 = $this->Post['brandpic2'];
		$picurl3 = $this->Post['brandpic3'];
		$brandorder1 = !empty($this->Post['brandorder1'])?intval($this->Post['brandorder1']):1;
		$brandorder2 = !empty($this->Post['brandorder2'])?intval($this->Post['brandorder2']):2;
		$brandorder3 = !empty($this->Post['brandorder3'])?intval($this->Post['brandorder3']):3;
		$addTime = time();
		
		if(empty($picurl1) || empty($picurl2) || empty($picurl3))
		{
			$this->Messager("必须同时输入3个图片url",-1);
		}
		
		/*$sql = "select * from `".TABLE_PREFIX."user_tag` where `name` = '{$tagname}' ";
	 	$query = $this->DatabaseHandler->Query($sql);
	  $taglist=$query->GetRow();
		
		if($taglist)
		{
			$this->Messager("{$tagname} 标签已经存在",-1);
		}*/
		$sql = "delete from `".TABLE_PREFIX."slide`";
		$this->DatabaseHandler->Query($sql);
		$sql = "insert into `".TABLE_PREFIX."slide` values ('','{$picurl1}','{$brandorder1}')";
		$this->DatabaseHandler->Query($sql);
        $sql = "insert into `".TABLE_PREFIX."slide` values ('','{$picurl2}','{$brandorder2}')";
		$this->DatabaseHandler->Query($sql);
		$sql = "insert into `".TABLE_PREFIX."slide` values ('','{$picurl3}','{$brandorder3}')";
		$this->DatabaseHandler->Query($sql);
		$this->Messager("添加成功",'admin.php?mod=brandslide');
	}
	
	function Modify()
	{ 
		$ids = (int) $this->Get['ids'];
		
		//$action = "admin.php?mod=brandtype&code=domodify";
		
		$sql = "select * from `".TABLE_PREFIX."brandclass` where `id` = '{$ids}' ";
	 	$query = $this->DatabaseHandler->Query($sql);
	    $val=$query->GetRow();
	   include $this->TemplateHandler->Template('admin/addcom');
	}
	
	function edittob()
	{ 
		$ids = (int) $this->Get['ids'];
		
		$sql = "select * from `".TABLE_PREFIX."brandclass` ";
	 	$query = $this->DatabaseHandler->Query($sql);
		 $brandtype_list=array();
		while($row = $query->GetRow())
		{	
			$brandtype_list[]=$row;
		}
		
		$sql = "select * from `".TABLE_PREFIX."tobacco` where `id` = '{$ids}' ";
	 	$query = $this->DatabaseHandler->Query($sql);
	    $val=$query->GetRow();
	   include $this->TemplateHandler->Template('admin/addtob');
	}
	
	function addcom()
	{ 
		if(!empty($this->Post['id']) && !empty($this->Post['company']) && !empty($this->Post['brandseri'])){
		$id =  $company = $this->Post['id'];
		 $company = $this->Post['company'];
		 $brandseri = $this->Post['brandseri'];
		$sql = "update `".TABLE_PREFIX."brandclass` set  `company` = '{$company}',`brandseri` = '{$brandseri}'  where `id`=".$id;	
		$this->DatabaseHandler->Query($sql);	
		$this->Messager("编辑成功",'admin.php?mod=brandtype');
		exit();
		}
		if(!empty($this->Post['company']) && !empty($this->Post['brandseri'])){
		 $company = $this->Post['company'];
		 $brandseri = $this->Post['brandseri'];
		$sql = "insert into `".TABLE_PREFIX."brandclass` values ('','{$company}','{$brandseri}','')";
		$this->DatabaseHandler->Query($sql);
		$this->Messager("添加成功",'admin.php?mod=brandtype');
		}else{
		
		include $this->TemplateHandler->Template('admin/addcom');
		}
	}
	
	function addtob()
	{ 
		if(!empty($this->Post['id']) && !empty($this->Post['tobacco_name'])){
		 $id =  $this->Post['id'];
		 $tobacco_name = $this->Post['tobacco_name'];
		 $w_price = $this->Post['w_price'];
		 $seriesID = $this->Post['seriesID'];
		 $r_price = $this->Post['r_price'];
		 $markettime = $this->Post['markettime'];
		 $tar = $this->Post['tar'];
		 $pinlei = $this->Post['pinlei'];
		 $standard = $this->Post['standard'];
		 $hBarcode = $this->Post['hBarcode'];
		 $tBarcode = $this->Post['tBarcode'];
		 $producttype = $this->Post['producttype'];
		 $package = $this->Post['package'];
		 $intro = $this->Post['intro'];
		$sql = "update `".TABLE_PREFIX."tobacco` set  `tobacco_name` = '{$tobacco_name}',`intro` = '{$intro}',`w_price`='{$w_price}',`r_price`='{$r_price}',`seriesID`='$seriesID',`markettime`='{$markettime}',`tar`='{$tar}',`pinlei`='{$pinlei}',`standard`='{$standard}',`hBarcode`='{$hBarcode}',`tBarcode`='{$tBarcode}',`producttype`='{$producttype}',`package`='{$package}'  where `id`=".$id;	
		$this->DatabaseHandler->Query($sql);	
		$this->Messager("编辑成功",'admin.php?mod=brandtype');
		exit();
		}
		if(!empty($this->Post['tobacco_name']) && !empty($this->Post['intro'])){
		 $tobacco_name = $this->Post['tobacco_name'];
		 $w_price = $this->Post['w_price'];
		 $seriesID = $this->Post['seriesID'];
		 if($seriesID=='0'){
		// $this->Messager("添加成功",'admin.php?mod=brandtype');
		 $this->Messager("请选择要添加的系列",-1);exit();
		 }
		 $r_price = $this->Post['r_price'];
		 $markettime = $this->Post['markettime'];
		 $tar = $this->Post['tar'];
		 $pinlei = $this->Post['pinlei'];
		 $standard = $this->Post['standard'];
		 $hBarcode = $this->Post['hBarcode'];
		 $tBarcode = $this->Post['tBarcode'];
		 $producttype = $this->Post['producttype'];
		 $package = $this->Post['package'];
		 $intro = $this->Post['intro'];
		 if($image=$_FILES['pic']['name']){
		 //$rnd = mt_rand(100, 999);
          $filename = date('YmdHis');
		  $savename=$filename.strtolower(strstr($_FILES['pic']['name'], '.'));
		  $s_pic= dirname(dirname(dirname(__FILE__)))."/images/brand/".$savename;
		  if(is_uploaded_file($_FILES['pic']['tmp_name'])){
             if(!move_uploaded_file($_FILES['pic']['tmp_name'], $s_pic)){
             echo '移动文件失败！';
              exit;
          }
         }else{
           echo 'problem!';
           exit;
         }
		 }
		$sql = "insert into `".TABLE_PREFIX."tobacco` values ('','{$tobacco_name}','{$intro}','{$savename}','{$w_price}','{$r_price}','$seriesID','{$markettime}','{$tar}','','{$pinlei}','{$standard}','{$hBarcode}','{$tBarcode}','{$producttype}','{$package}')";
		$this->DatabaseHandler->Query($sql);
		$insert_id = $this->DatabaseHandler->insert_id();
		//die($insert_id); 
		if($morepic = $this->Post['morepic']){
		 $sql = "insert into `".TABLE_PREFIX."morepic` values ('','$insert_id','{$morepic}')";
		   $this->DatabaseHandler->Query($sql);
		 }
		$this->Messager("添加成功",'admin.php?mod=brandtype');
		}else{
		$sql = "select * from `".TABLE_PREFIX."brandclass` ";
	 	$query = $this->DatabaseHandler->Query($sql);
		 $brandtype_list=array();
		while($row = $query->GetRow())
		{	
			$brandtype_list[]=$row;
		}
		include $this->TemplateHandler->Template('admin/addtob');
		}
	}
	
	function tobaccoindex()
	{
		$ids = (int) $this->Get['ids'];
		$per_page_num = min(500,max((int) $_GET['per_page_num'],(int) $_GET['pn'],10));
		$query_link = 'admin.php?mod=brandtype&code=tobaccoindex';
		$sql = " select count(*) as `total_record` from jy_brandclass as a left join jy_tobacco as b on a.id=b.seriesID where a.id=$ids and b.tobacco_name<>''";	
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$page_arr = page($total_record,$per_page_num,$query_link,array('return'=>'array',),'20 50 100 200,500');
		$sql = "select b.id, a.company,a.brandseri,b.tobacco_name from jy_brandclass as a left join jy_tobacco as b on a.id=b.seriesID where a.id=$ids and b.tobacco_name<>''";
	 	$query = $this->DatabaseHandler->Query($sql);
	     $tobacco_list=array(); 
		while($row = $query->GetRow())
		{	
			$tobacco_list[]=$row;
		}
		
		include $this->TemplateHandler->Template('admin/tobaccoindex');
	}
	
	 function liuyan()
	{
		$per_page_num = min(500,max((int) $_GET['per_page_num'],(int) $_GET['pn'],10));
		$query_link = 'admin.php?mod=brandtype&code=liuyan';
		$sql = " select count(*) as `total_record` from jy_liuyan ";	
		$query = $this->DatabaseHandler->Query($sql);
        extract($query->GetRow());
		$page_arr = page($total_record,$per_page_num,$query_link,array('return'=>'array',),'20 50 100 200,500');
		$sql = "select * from jy_liuyan order by posttime desc";
	 	$query = $this->DatabaseHandler->Query($sql);
	     $liuyan_list=array(); 
		while($row = $query->GetRow())
		{	
			$liuyan_list[]=$row;
		}
		
		include $this->TemplateHandler->Template('admin/liuyan');
	}
	
	function DoModify()
	{
		$tagid = (int) $this->Post['tagid'];
		$tagname = strip_tags($this->Post['tagname']);
		
		$sql = "select * from `".TABLE_PREFIX."user_tag` where `name` = '{$tagname}' ";
	 	$query = $this->DatabaseHandler->Query($sql);
	  $taglist=$query->GetRow();
	  
		if($taglist)
		{
			$this->Messager("{$tagname} 标签已经存在",-1);
		}
		
		$sql = "update `".TABLE_PREFIX."user_tag` set  `name` = '{$tagname}'  where `id`=".$tagid;	
		$this->DatabaseHandler->Query($sql);	
		
		$this->Messager("编辑成功",'admin.php?mod=user_tag');
	}
	
	function Delete()
	{
		$ids = (array) ($this->Post['ids'] ? $this->Post['ids'] : $this->Get['ids']);
		$media_count = count($ids);
		if(!$ids) {
			$this->Messager("请指定要删除的对象");
		}
		
		$sql = "delete from `".TABLE_PREFIX."brandclass` where `id` in(".implode(",",$ids).")";
		$this->DatabaseHandler->Query($sql);			
	    $this->Messager($return ? $return : "操作成功");
		
	}
	function deletetob()
	{
		$ids = (array) ($this->Post['ids'] ? $this->Post['ids'] : $this->Get['ids']);
		$media_count = count($ids);
		if(!$ids) {
			$this->Messager("请指定要删除的对象");
		}
		
		$sql = "delete from `".TABLE_PREFIX."tobacco` where `id` in(".implode(",",$ids).")";
		$this->DatabaseHandler->Query($sql);			
	    $this->Messager($return ? $return : "操作成功");
		
	}

}

?>
